package com.example.demo.controller;

import com.example.demo.entity.TbBlogEntity;
import com.example.demo.entity.request.Comment;
import com.example.demo.service.TbBlogService;
import com.example.demo.service.TbMessageForBlogService;
import com.example.demo.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api("博客")
@RestController
@RequestMapping("/api/blog")
public class BlogController {

    TbBlogService blogService;
    TbMessageForBlogService messageForBlogService;

    @Autowired
    public void setBlogService(TbBlogService blogService) {
        this.blogService = blogService;
    }

    @Autowired
    public void setMessageForBlogService(TbMessageForBlogService messageForBlogService) {
        this.messageForBlogService = messageForBlogService;
    }

    @ApiOperation("分页获取博客文章列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "index", value = "当前页"),
            @ApiImplicitParam(name = "size", value = "页大小")
    })
    @GetMapping("getBlogsByPage")
    public Result getBlogsByPage(Integer index, Integer size) {
        Map<String, Object> result = blogService.getBlogsByPage(index, size);
        return new Result(0, "success", result);
    }

    @ApiOperation("文章点赞")
    @ApiImplicitParam(name = "id", value = "文章id")
    @PostMapping("like")
    public Result like(Integer id) {
        if(id == null) {
            return new Result(1, "id未传");
        }
        blogService.like(id);
        return new Result(0, "success");
    }

    @ApiOperation("模糊搜索文章（内容、标题）")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "value", name = "搜索的内容"),
            @ApiImplicitParam(value = "index", name = "当前页"),
            @ApiImplicitParam(value = "size", name = "页大小")
    })
    @GetMapping("getBlogByLike")
    public Result getBlogByLike(String value, Integer index, Integer size) {
        Map<String, Object> blogEntities = blogService.getBlogByLike(value, index, size);
        return new Result(0, "success", blogEntities);
    }
    @GetMapping("getBlogByLikeId")
    public Result getBlogByLikeId(Integer id) {
        List<TbBlogEntity> result = blogService.getBlogByLikeId(id);
        return new Result(0, "success", result);
    }

    @ApiOperation("根据标签查找文章")
    @ApiImplicitParam(name = "tagName", value = "标签名")
    @GetMapping("getBlogByTagName")
    public Result getBlogByTagName(String tagName, Integer index, Integer size) {
        Map<String, Object> blogEntityList = blogService.getBlogsByTagName("%" + tagName + "%", index, size);
        return new Result(0, "success", blogEntityList);
    }

    @ApiOperation("获取单篇文章评论列表")
    @ApiImplicitParam(name = "id", value = "文章ID")
    @GetMapping("getBlogMessageById")
    public Result getBlogMessageById(Integer id, Integer index, Integer size) {
        if(id == null) return new Result(-1, "缺少参数");
        Map<String, Object> message = blogService.getBlogMessageById(id, index, size);
        return new Result(0, "success", message);
    }

    @ApiOperation("文章留言")
    @PostMapping("comment")
    public Result comment(@RequestBody @Validated Comment comment) {
        return new Result(0, "success", messageForBlogService.save(comment));
    }

    @ApiOperation("评论点赞")
    @PostMapping("likeMessage")
    public Result likeMessage(Integer id) {
        messageForBlogService.likeMessage(id);
        return new Result(0, "message");
    }
}
